package com.gujiangbo.chapter16;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/4/11 14:20
 * @Version: 1.0
 * @Description:
 */
public class JavaStw {

    public static void main(String[] args) {
        Normal normal = new Normal();
        WorkThread work = new WorkThread();
        normal.start();
        work.start();

    }


    public static class WorkThread extends Thread {
        List<byte[]> list = new ArrayList<byte[]>();

        @Override
        public void run() {
            while (true) {
                for (int i = 0; i < 1000; i++) {
                    byte[] buffer = new byte[1024];
                    list.add(buffer);
                }
                if (list.size() > 10000) {
                    list.clear();
                    System.gc();
                }
            }
        }
    }

    public static class Normal extends Thread {

        public final long startTime = System.currentTimeMillis();

        @Override
        public void run() {

            while (true) {
                long costTime = System.currentTimeMillis() - startTime;
                System.out.println(costTime / 1000 + "." + costTime % 1000);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    }

}


